<?php
/*
	首页
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$mid=$_RQ['mid'];
	if(!$mid && !$_RQ['ismy']){
		exi('该用户未绑定会员ID！','error');
	}
	$member=$this->model('member')->get_member($mid);
	$member['credit1passtime']=date('Y年m月d日',time()+7*24*3600);
	$this->session('mid',$member['mid']);
	$this->model('siimg')->add_sivideolog($item['mid']);
	$return['member']=$member;
	$return['params']=array('type'=>$_RQ['type']?:2);
	$return['list']=array();
	$sharelink='jy_ailiao/simivideo//type='.$_RQ['type'].'&fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$return['pluginset']['share_title']?:'私密视频',
			'desc'=>$return['pluginset']['share_desc']?:'私密视频，成就天下姻缘！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($return['pluginset']['share_image'])
	);
	set_html('title','私密视频');
	exi($return);
}
if($_RQ['op']=='getquanlog'){
	//获取最近50家店
	if(intval($_RQ['lat'])){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$lat_lng=$this->session('lat_lng');
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
		'noshow'=>0,
		'simi'=>1,
	);
	if($this->session('mid')){
		$params['mid']=$this->session('mid');
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,8),
		'fields'=>'',
		'key'=>'id',
		'orderby'=>$orderby,
		'orderbykey'=>$orderbykey,
	);
	$logs=$this->model('member')->getlogs($parameter,6,$lat_lng,intval($_RQ['page']),intval($_RQ['juli']));
	foreach($logs as $k=>&$v){
		$v['logo']=$v['logo']?tomedia($v['logo']):'';
		$v['topimg']=$v['topimg']?tomedia($v['topimg']):'';
		$v['createtime']=date('Y-m-d',$v['createtime']);
		$v['images']=json_decode($v['images'],true);
		foreach($v['images'] as &$iv){
			$iv=tomedia($iv);
		}
	}
	$return['list']=array_values($logs);
	exi($return);
}
if($_RQ['op']=='looksiimg'){
	$postdata=get_postdata();
	$member=$this->model('member')->get_member();
	if($member['siminum']<=0){
		exi('目前剩余查看私密照次数不够！','error');
	}
	$item=pdo_get('jy_ailiao_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$postdata['id']));
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'tomid'=>$item['mid'],
			'sn'=>get_ordersn('jy_ailiao_sivideoorder','sn'),
			'price'=>0,
			'status'=>1,
			'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_ailiao_sivideoorder',$insert);
	pdo_update('jy_ailiao_member',array('siminum -='=>1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	exi('查看成功！');
}
if($_RQ['op']=='buysiimg'){
	$postdata=get_postdata();
	$item=pdo_get('jy_ailiao_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$postdata['id']));
	$sivideo=$this->plugin_setting('sivideo');
	$price=$sivideo['price'];
	if($price>0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'tomid'=>$item['mid'],
			'sn'=>get_ordersn('jy_ailiao_sivideoorder','sn'),
			'price'=>$price,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_ailiao_sivideoorder',$insert);
		$id=pdo_insertid();
			$params=array(
					'id'=>$id,
					'title'=>'付费查看私密视频',
					'fee'=>$price,
					'plugin'=>'jy_ailiao',
					'no_type'=>array('hdfk'),
					'back_url'=>'jy_ailiao/simivideo/payresult/',
					'success_url'=>'jy_ailiao/index/',
					'error_url'=>'jy_ailiao/index/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			pdo_update('jy_ailiao_sivideoorder',array('uniacid'=>$_SESSION['uniacid'],'payid'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
	}else{
		exi('付费失败，价格未设置！');
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_ailiao_sivideoorder',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	$siimg=$this->plugin_setting('siimg');
	$rate=intval($siimg['rate']);
	$ratefee=$rate*$order['price']/100;
	pdo_update('jy_ailiao_sivideoorder',array('status'=>1,'ratefee'=>$ratefee,'istixian'=>1,'paytype'=>$payresult['pay_type'],'transid'=>$payresult['transaction_id']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$payresult['id']));
	$money=$order['price']-$ratefee;
	cfc('core')->member_credit($order['mid'],'credit2',$money,true,'jy_ailiao','付费查看私密视频');
	exi('支付成功','success','jy_ailiao/index/');
}
?>